<template>
  <div class="login-container">
    <el-form class="myform" ref="myform" :model="form" :rules="rules">
      <h3>iHRM后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          style="width:100%"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入用户密码"
          show-password
          prefix-icon="el-icon-lock"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="onSubmit" :loading="loading"
          >登录</el-button
        >
      </el-form-item>
      <div class="mrck">还没有注册，请先去注册</div>
    </el-form>
  </div>
</template>

<script>
// import { loginApi } from '@/api/user'
// import { setToken } from '@/utils/auth'
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('手机号码不正确'))
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6-16 个字符', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    ...mapActions({
      userlogin: 'user/login'
    }),
    onSubmit () {
      this.$refs.myform.validate(async value => {
        if (value) {
          // let res = await loginApi(this.form)
          // console.log(res)
          // setToken(res.data)
          try {
            this.loading = true
            await this.userlogin(this.form)
            const _topath = this.$route.query._topath
            if (_topath) {
              this.$router.push(_topath)
            } else {
              this.$router.push('/')
            }
            this.$message.success('登录成功')
          } catch (error) {
            // console.log(error)
          } finally {
            this.loading = false
          }
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .myform {
    margin: 0 auto;
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -60%);
    h3 {
      font-size: 40px;
      color: #fff;
      font-weight: 300;
      text-align: center;
      letter-spacing: 8px;
    }
    .btn {
      width: 100%;
      background-color: #3e7efd;
      color: #fff;
      border: none;
      height: 60px;
      font-size: 20px;
    }
    .el-input__inner {
      height: 60px;
    }
    .mrck {
      color: #fff;
      text-align: center;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
